﻿using System.Text;
using Microsoft.AspNetCore.Mvc;
using static System.Formats.Asn1.AsnWriter;

namespace WebApplicationKG.Controllers
{
    public class Chp07Controller : Controller
    {
        /// <summary>
        /// 员工类
        /// </summary>
        public class Employee
        {
            public int EmpId { get; set; }
            public string DeptName { get; set; } = "";
            public string RealName { get; set; } = "";
            public string Phone { get; set; } = "";
            public string Sex { get; set; } = "";
            public string Hobby { get; set; } = "";
        }

        #region 模型绑定获取表单数据


        public IActionResult Demo01()
        {
            return View();
        }
        public IActionResult Demo01Show(Employee emp)
        {
            //可以使用Request对象来接受
            //string str = "员工基本信息如下：";
            //str += "<div>员工姓名："+Request.Form["RealName"]+"</div>";
            //str += "<div>所属部门：" + Request.Form["DeptName"] + "</div>";
            //str += "<div>员工电话：" + Request.Form["Phone"] + "</div>";
            //str += "<div>员工性别：" + Request.Form["Sex"] + "</div>";
            //str += "<div>员工爱好：" + Request.Form["Hobby"] + "</div>";
            //ViewBag.Info = str;

            //可以直接通过模型绑定表单数据（表单元素名称必须和对象属性名称相同，并且）
            string str = "员工基本信息如下：";
            str += "<div>员工姓名：" + emp.RealName + "</div>";
            str += "<div>所属部门：" + emp.DeptName + "</div>";
            str += "<div>员工电话：" + emp.Phone + "</div>";
            str += "<div>员工性别：" + emp.Sex + "</div>";
            str += "<div>员工爱好：" + emp.Hobby + "</div>";
            ViewBag.Info = str;

            return View();
        }

        #endregion

        #region 基本类型获取数据
        public IActionResult Demo02(string RealName, int? Score)
        {
            if (RealName != null && RealName.Equals(""))
            {
                string str = "表单提交信息如下：";
                str += "<div>姓名：" + RealName + "</div>";
                str += "<div>分数：" + Score + "</div>";
                ViewBag.Info = str;
            }

            return View();
        }

        #endregion
        public IActionResult Demo03()
        {//需要设置form enctype="multipart/form-data"才能正常提交文件
            return View();
        }

        public IActionResult Demo03Save(IFormFile fileImg)
        {   //获取文件名
            string fileName=fileImg.FileName;

           //获取后缀名
            string fileExt=Path.GetExtension(fileName);

            //判断文件是否合法
            //if(fileExt.ToLower() !=".jpg" && fileExt.ToLower() !=".png" && fileExt.ToLower() != ".gif")
            //    return Content("<script>alert('上传文件格式不正确！');window.location.href='Demo03';</script>","text/html",Encoding.UTF8);

            //重新生成文件名
            fileName = Guid.NewGuid().ToString()+fileExt;

            //上传文件到服务器
            string filePath = Path.Combine(Directory.GetCurrentDirectory(),"wwwroot/uploads",fileName);
            using(var stream=new FileStream(filePath, FileMode.Create))
            {
                fileImg.CopyTo(stream);
            }
            TempData["FileName"] = fileName;
            return RedirectToAction("Demo03");
           
            ////根据文件内容判断文件格式
            //byte[] fileBytes = new byte[fileImg.Length];




          
        }


    }
}
